﻿using System;
using SE6;
public enum TopicState
{
	Normal=0,
	Hidden=1,
	Excellent=2,
}
public class Topic
{
	//从数据库中创建
	public Topic(Int32 id,String title,String author,
		Int32 viewCount,Int32 replyCount,
		String lastReplyTime,String lastReplyAuthor,TopicState state,Boolean top)
	{
		_id=id;
		_title = title;
		_author = author;
		_viewCount = viewCount;
		_replyCount = replyCount;
		_lastReplyTime = lastReplyTime;
		_lastReplyAuthor = lastReplyAuthor;
		_state=state;
        _top = top;
	}
	//从用户输入，添加新主题
	public Topic(String title,String author)
	{
		_title = title;
		_author = author;
		_lastReplyAuthor = _author;
		_id = 0;
		_viewCount = 0;
		_replyCount = 0;
		_lastReplyTime = DateTime.Now.ToString();
		_state = TopicState.Normal;
        _top = false;
	}
	//以XHTML标签格式化Topic各项内容
	public String Render(Boolean isOdd)
	{
		String css = isOdd ? "class='odd'" : "";
		return 
				"<tr "+css+">" +
					"<td class='viewCnt'>"+_viewCount+"</td>" +
					"<td class='replyCnt'>"+_replyCount+"</td>" +
					"<td class='topicTitle'>" + GetUrl() + GetTitleDecorate() +	"</td>" +
					"<td class='author'>" + UserMan.GetUserProfileURL(_author) + "</td>" +
					"<td class='lastReply'><span class='time'>" + _lastReplyTime +"</span>"+
						UserMan.GetUserProfileURL(_lastReplyAuthor) + "</td>" +
				"</tr>";
	}
	public String GetDeleteURL()
	{
		return "<a class='button' href='par.aspx?t=8&id=" + _id+"'>删除该帖(请三思)</a>";
	}
	public void ChangeTop()
	{
		_top = !_top;
	}
	public Boolean IsExcellent()
	{
		return (_state & TopicState.Excellent) != 0;
	}
	public void ChangeExcellent()
	{
		if (IsExcellent())
			_state &= ~TopicState.Excellent;
		else
			_state |= TopicState.Excellent;
	}
	public String ReadableTime()
	{
		return "";
	}
	//以XHTML标签格式化Topic的标题栏
	public static String RenderHeader()
	{
		return
				"<tr class='odd'>" +
					"<td>点击</td>"+
					"<td>回复</td>"+
					"<td>标题</td>"+
					"<td>作者</td>" +
					"<td>最后回复</td>"+
				"</tr>";
	}
	private String GetUrl()
	{
		return "<a href='ViewTopicDetail.aspx?id=" + _id +"&cp=0'>"+_title+"</a>";
	}
	//主题编号
	public Int32 ID
	{
		get { return _id; }
	}
	//标题
	public String Title
	{
		get { return _title; }
	}
	//浏览数
	public Int32 ViewCount
	{
		get{return _viewCount;}
	}
	//回复数
	public Int32 ReplyCount
	{
		get{return _replyCount;}
	}
	//最后回复发表时间
	public String LastReplyTime
	{
		get { return _lastReplyTime; }
	}
	//最后回复作者
	public String LastReplyAuthor
	{
		get { return _lastReplyAuthor; }
	}
	//主题状态
	public TopicState State
	{
		get { return _state; }
	}
	//作者
	public String Author
	{
		get { return _author; }
	}
    public Boolean Top
    {
        get { return _top; }
    }
	private String GetTitleDecorate()
	{
		return 
			((_state&TopicState.Excellent)!=0?"<span class='red'>[精华]</span>":"")+
			(_top?"<span class='green'>[置顶]</span>":"");
	}
	private Int32 _id;//主题编号
	private String _title;//标题
	private String _author;//作者
	private Int32 _viewCount;//浏览数
	private Int32 _replyCount;//回复数
	private String _lastReplyTime;//最后回复时间
	private String _lastReplyAuthor;//最后回复作者
	private TopicState _state;//主题状态
    private Boolean _top;//主题置顶状态
}

